/**
 * 
 */
package org.compiere.process;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.logging.Level;

import org.compiere.apps.ADialog;
import org.compiere.model.MBPMForm;
import org.compiere.model.MBPMFormCell;
import org.compiere.model.MBPMFormLine;
import org.compiere.model.MBPMFormValues;
import org.compiere.model.MBSCCard;
import org.compiere.model.MBSCCardLine;
import org.compiere.model.MBSCCardLineLink;
import org.compiere.model.MParameter;
import org.compiere.model.MParameterLine;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 * @author Y.Ibrayev
 *
 */
public class CalcCardLine extends SvrProcess {

	private int BSC_CardLine_ID = 0;
	
	/* (non-Javadoc)
	 * @see org.compiere.process.SvrProcess#prepare()
	 */
	@Override
	protected void prepare() {
		ProcessInfoParameter[] para = getParameter();
		for(int i=0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if(para[i].getParameter() == null ) {
				;
			} else if(name.equals("BSC_CardLine_ID")) {
				BigDecimal tmp = (BigDecimal) para[i].getParameter();
				BSC_CardLine_ID = tmp.intValue() ;
			} else {
				log.log(Level.SEVERE,"CalcCardLine: Unknown parameter - "+name);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.compiere.process.SvrProcess#doIt()
	 */
	@Override
	protected String doIt() throws Exception {
		log.log(Level.INFO,"CalcCardLine: BSC_CardLine_ID - "+BSC_CardLine_ID);
		if (BSC_CardLine_ID > 0) {
			MBSCCardLine cardLine = new MBSCCardLine(Env.getCtx(),BSC_CardLine_ID,null);
			//-->add by Aslanbek			
			String value = "0";
			int diff = 0;
			for(MBSCCardLine line : cardLine.getLinkLines())
			{
				if(line.getValue()!=null)
				{
					value = String.valueOf(Integer.valueOf(value)+Integer.valueOf(line.getValue()));
					diff++;
				}
			}
			if(!value.equals("0")){
				value = String.valueOf(Integer.valueOf(value)/diff);
				cardLine.setValue(value);
			}
			//<--
			if (cardLine.save()) {
				BigDecimal result = cardLine.calculate(false);
				log.log(Level.INFO,"CalcCardLine: result - "+result.toString());
				cardLine.setValueNumber(result);
				cardLine.save();
			} else {
				ADialog.info(25, null, "Но могу посчитать линию карты ССП");
			}
			
			//фактические значение на карте ССП закидываем на указанную форму
			if(cardLine.getBPM_FormLine()!=null)
			{
				MBPMFormValues formValues = null;
				
				if(cardLine.getBPM_FormLine().getBPM_Form().getBPM_FormCode_ID()==1000019){ //форма 11
					
					for(MBPMFormCell cell: MBPMFormLine.getLines(getCtx(), cardLine.getBPM_FormLine_ID(), get_TrxName()))
					{	
						formValues = MBPMFormValues.getFormValueLine(cardLine.getBPM_FormLine_ID(), cell.getBPM_FormColumn_ID()
								, cardLine.getBPM_FormLine().getBPM_Form().getBPM_VersionBudget_ID(), 1000020, cell.getBPM_FormCell_ID());
						if(cell.getBPM_FormColumn().getC_Year_ID()>0)
						{
							if(cell.getBPM_FormColumn().getC_Year().getFiscalYear().equals(cardLine.getBSC_Card().getC_Period().getC_Year().getFiscalYear()))
							{
								if(cell.getBPM_FormColumn_ID()==1001765 //Значение показателей по годам, 2011 г.  Факт 
								   || cell.getBPM_FormColumn_ID() == 1001766 //Значение показателей по годам, 2012 г. Факт
								   || cell.getBPM_FormColumn_ID() == 1001767 //Значение показателей по годам, 2013 г.  Факт 
								   || cell.getBPM_FormColumn_ID() == 1000229 //Значение показателей по годам, 2014 г. Факт
								   || cell.getBPM_FormColumn_ID() == 1001769 )//Значение показателей по годам, 2015 г. Факт
								{
									if(formValues!=null)
										formValues.setCellValue(cardLine.getValue());
									else{
										formValues = new MBPMFormValues(getCtx(), 0, get_TrxName());
										formValues.set_ValueOfColumn("AD_Client_ID", Env.getAD_Client_ID(getCtx()));
										formValues.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
										formValues.setBPM_Form_ID(cardLine.getBPM_FormLine().getBPM_Form_ID());
										formValues.setBPM_FormCell_ID(cell.getBPM_FormCell_ID());
										formValues.setBPM_FormLine_ID(cardLine.getBPM_FormLine_ID());
										formValues.setBPM_FormColumn_ID(cell.getBPM_FormColumn_ID());
										formValues.setBPM_VersionBudget_ID(cardLine.getBPM_FormLine().getBPM_Form().getBPM_VersionBudget_ID());
										formValues.setBPM_Project_ID(1000020);
										formValues.setCellValue(cardLine.getValue());
									}
									formValues.saveEx();
								}
							}
						}
					}
				}
				else if(cardLine.getBPM_FormLine().getBPM_Form().getBPM_FormCode_ID()==1000020)// форма 12
				{
					for(MBPMFormCell cell: MBPMFormLine.getLines(getCtx(), cardLine.getBPM_FormLine_ID(), get_TrxName()))
					{	
						formValues = MBPMFormValues.getFormValueLine(cardLine.getBPM_FormLine_ID(), cell.getBPM_FormColumn_ID()
								, cardLine.getBPM_FormLine().getBPM_Form().getBPM_VersionBudget_ID(), 1000020, cell.getBPM_FormCell_ID());
						if(cell.getBPM_FormColumn().getC_Year_ID()>0)
						{
							if(cell.getBPM_FormColumn().getC_Year().getFiscalYear().equals(cardLine.getBSC_Card().getC_Period().getC_Year().getFiscalYear()))
							{
								if(cell.getBPM_FormColumn_ID()==1001718 //Значение показателей по годам, 2011 г.  Факт 
								   || cell.getBPM_FormColumn_ID() == 1001720 //Значение показателей по годам, 2012 г. Факт
								   || cell.getBPM_FormColumn_ID() == 1001722 //Значение показателей по годам, 2013 г.  Факт 
								   || cell.getBPM_FormColumn_ID() == 1001724 //Значение показателей по годам, 2014 г. Факт
								   || cell.getBPM_FormColumn_ID() == 1001726 )//Значение показателей по годам, 2015 г. Факт
								{
									if(formValues!=null)
										formValues.setCellValue(cardLine.getValue());
									else{
										formValues = new MBPMFormValues(getCtx(), 0, get_TrxName());
										formValues.set_ValueOfColumn("AD_Client_ID", Env.getAD_Client_ID(getCtx()));
										formValues.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
										formValues.setBPM_Form_ID(cardLine.getBPM_FormLine().getBPM_Form_ID());
										formValues.setBPM_FormCell_ID(cell.getBPM_FormCell_ID());
										formValues.setBPM_FormLine_ID(cardLine.getBPM_FormLine_ID());
										formValues.setBPM_FormColumn_ID(cell.getBPM_FormColumn_ID());
										formValues.setBPM_VersionBudget_ID(cardLine.getBPM_FormLine().getBPM_Form().getBPM_VersionBudget_ID());
										formValues.setBPM_Project_ID(1000020);
										formValues.setCellValue(cardLine.getValue());
									}
									formValues.saveEx();
								}
							}
						}
					}
				}
			}
			//
		}
		return Msg.translate(Env.getCtx(), "Success");
	}

}
